<template>
  <div>
    <el-form :inline="true">
      <el-form-item label="高中">
        <HighSchoolSelect v-model="school" />
      </el-form-item>
      <el-form-item label="地区">
        <AreaSelect v-model="area" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleSearch">搜索</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" :disabled="multipleSelection.length===0" @click="handleSelect">推荐</el-button>
      </el-form-item>
    </el-form>
    <el-table ref="table" v-loading="table.loading" border :data="table.data" @selection-change="handleSelectionChange">
      <el-table-column
        type="selection"
        width="55"
      />
      <el-table-column label="昵称" prop="nick_name" />
      <el-table-column label="姓名" prop="name" />
      <el-table-column label="学校" prop="school.title" />
      <el-table-column label="地区" prop="area.labels" />
    </el-table>
    <WrapPagination>
      <el-pagination
        class="pagination"
        :current-page="table.page"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="table.size"
        layout="total, prev, pager, next"
        :total="table.total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </WrapPagination>
  </div>
</template>

<script>
import { TableMixin } from '../../../components/mixin'
import { showUnFollowUsers, createFollowUsers } from '../../../api/admin'

export default {
  components: {
    AreaSelect: () => import('../../../components/AreaSelect'),
    HighSchoolSelect: () => import('../../../components/HighSchoolSelect')
  },
  mixins: [TableMixin],
  data() {
    return {
      multipleSelection: [],
      school: '',
      area: []
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    handleSelect() {
      if (this.multipleSelection.length === 0) {
        this.$message.warning('请先选择要推荐的用户')
      } else {
        createFollowUsers({
          users: this.multipleSelection,
          academy_code: this.$route.params.academy_code
        }).then(() => {
          this.table.page = 1
          this.getList()
          this.$emit('notify')
        })
      }
    },
    handleSelectionChange(val) {
      this.multipleSelection = val.map(v => {
        return v.id
      })
    },
    getList() {
      this.wrapResourceList(showUnFollowUsers, {
        academy_code: this.$route.params.academy_code,
        area: this.area,
        school: this.school
      })
    },
    handleSearch() {
      this.table.page = 1
      this.getList()
    }
  }
}
</script>

<style scoped>

</style>
